package com.jucai.pppeiyouclient.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.fm.openinstall.OpenInstall;
import com.fm.openinstall.listener.AppInstallListener;
import com.fm.openinstall.model.AppData;
import com.fm.openinstall.model.Error;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.utils.FirstLaunchUtils;
import com.jucai.pppeiyouclient.utils.LoginUtils;
import com.jucai.pppeiyouclient.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;


public class SplashActivity extends BaseActivity implements AppInstallListener {
    private Handler mHandler = new Handler();
    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_splash);
        OpenInstall.getInstall(this);
        mHandler.postDelayed(new JumpLogin(), 3000);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void loadNetData() {

    }

    @Override
    protected void setListener() {

    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onInstallFinish(AppData appData, Error error) {
        if (error == null) {

            try {
                //获取渠道数据
                LoginUtils.saveOpenInstallChannel(SplashActivity.this, appData.getChannel());
                JSONObject jsonObject = new JSONObject(appData.getData());
                String member_extension_id = jsonObject.has("id") ? jsonObject.getString("id") : "";
                LoginUtils.saveOpenInstallId(SplashActivity.this, member_extension_id);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            //获取个性化安装数据
        } else {

        }
    }

    private class JumpLogin implements Runnable {
        @Override
        public void run() {
            jump();
        }
    }

    private void jump() {
        if (!FirstLaunchUtils.isLaunched(SplashActivity.this)) {
            Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
            startActivity(intent);
            setResult(Activity.RESULT_OK);
            finish();
        } else {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }
}

